﻿namespace LeetCodeProject._0001_1000._001_100._41_50._050;

public class Solution
{
    public double MyPow(double x, int n)
    {
        if (n == 0)
            return 1;
        if (n < 0)
        {
            x = 1 / x;
            n = n == int.MinValue ? 0 : -n;
        }

        var result = 1.0d;
        var power = 1;
        while ((power <= n || n == 0) && power > 0)
        {
            if ((n & power) != 0)
                result *= x;
            power <<= 1;
            x *= x;
        }

        if (n == 0)
            result *= x;
        return result;
    }
}